



import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Point;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Calendar;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author dgelman
 */
@SuppressWarnings("unused")
public class Instructions extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static int QUIZ_QUESTIONS = 9;
	static int FIRST_PANE = 4;

	boolean debug = false;
	boolean testing = false;

	JTextField[] answer_boxes;
	JButton[] checkButton;
	JCheckBox[] question5boxes = new JCheckBox[5];
	betterBoolean[] canAdvance;

	Socket server;
	Client client;

	JPanel mainPanel, top, centerPanel, bottom, quizWrapper;
	JLabel splash;
	JScrollPane scroll;
	GridBagLayout layout;
	GridBagConstraints constraints;
	JButton start, back;

	/**
	 * Creates a new instance of Instructions
	 */
	public Instructions(Client client) {

		super();
		this.client = client;
		canAdvance = new betterBoolean[5];
		for (int i = 0; i < canAdvance.length; i++) {
			canAdvance[i] = new betterBoolean(false);
		}

		setExtendedState(Frame.MAXIMIZED_BOTH);
		setUndecorated(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainPanel = new JPanel();

		mainPanel.setLayout(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
		mainPanel.setBackground(Color.white);
		mainPanel.setOpaque(true);

		// header
		top = new JPanel();
		top.setBackground(Color.white);
		top.setOpaque(true);
		top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
		JPanel header1 = new JPanel();
		JLabel header1L = new JLabel(
				"University of California San Diego, Economics Department");
		header1.add(header1L);
		header1.setBackground(new Color(100, 149, 237));
		header1.setOpaque(true);
		JPanel header2 = new JPanel();
		JLabel header2L = new JLabel("Instructions");
		header2.add(header2L);
		header2.setBackground(new Color(255, 215, 0));
		header2.setOpaque(true);
		header1.setAlignmentX(Component.CENTER_ALIGNMENT);
		header2.setAlignmentX(Component.CENTER_ALIGNMENT);
		top.add(header1);
		top.add(header2);

		// centerPanel
		centerPanel = new JPanel();
		layout = new GridBagLayout();
		constraints = new GridBagConstraints();
		centerPanel.setLayout(layout);
		centerPanel.setBackground(Color.white);
		centerPanel.setOpaque(true);

		// mainpanel
		JPanel text = new JPanel();
		text.setLayout(new BoxLayout(text, BoxLayout.PAGE_AXIS));
		text.setBackground(Color.white);
		text.setOpaque(true);

		JLabel title = new JLabel(
				"<html><font size=+2>Experiment Instructions</font></html>");
		title.setHorizontalAlignment(JLabel.CENTER);
		title.setAlignmentX(Component.CENTER_ALIGNMENT);

		/*Create JLabel containing screen shot of gains choice task*/
		JLabel screenshot =  new JLabel(new ImageIcon(getClass().getResource(
				"/images/1.gif")));
		screenshot.setPreferredSize(new Dimension(601, 550));
		screenshot.setMinimumSize(new Dimension(601, 550));
		screenshot.setMaximumSize(new Dimension(601, 550));
		screenshot.setHorizontalAlignment(JLabel.CENTER);
		screenshot.setAlignmentX(Component.CENTER_ALIGNMENT);

//		/* Create Gamble and GamblePane to demo in instructions */
		Pledge demop = new Pledge(.5, 1000, 2000, 0);
		RoundPane demo = new RoundPane(demop, 5000, Client.A, Client.B, Client.alpha, 0);
		demo.setMinimumSize(new Dimension(600, 550));
		demo.setMaximumSize(new Dimension(600, 550));

		JTextPane[] instructions = new JTextPane[6];
		for (int i = 0; i < instructions.length; i++) {
			instructions[i] = new JTextPane();
			instructions[i].setAlignmentX(Component.CENTER_ALIGNMENT);
			instructions[i].setEditable(false);
			instructions[i].setContentType("text/html");
		}

		instructions[0]
				.setText("<html><font face=\"Lucida Grande\" size=\"-1\"><p>Just for agreeing to participate today, "
						+ "you will receive a $"+Client.B+" \"thank you\" payment. The decisions you make today will give you "
						+ "a chance to add to this amount. Whether "
						+ "you gain more money will depend in part on your decisions and in part on chance. We will pay you "
						+ "your gains in cash at the end of the session today. "
						+ "<p>Today you face a series of rounds. Each round involves selecting a pledge to make. "
						+ "The pledges per round and the reward for matching them will vary. "
						+ "<p>A pledge might look like this:<p></font></html>");
		instructions[1]
				.setText("<html><font face=\"Lucida Grande\" size=\"-1\">" 
						+ "<p>This would represent a scenario where you were given $E 2000.00 "
						+"to start with and have decided to pledge a match of $E 1.00 for $E 1.00 up to "
						+"a total giving of $E 500.00, and you will gain $E 1224.74 if you are matched.");
		instructions[2]
				.setText("<html><font face=\"Lucida Grande\" size=\"-1\"><p>Each round will differ according to the "
						+ "amount of money you have left, and the amount of money you are willing to pledge more. "
						+ "<p>For each round, you'll need to tell us a certain rate to match your pledge to, and a maximum amount to give. "
						+ "<p>A round of pledges might look like this.  Notice, you see all available pledges in the "
						+ "graph by moving the red dot around -- give it a try!<p><p></font></html>");
		instructions[3]
				.setText("<html><font face=\"Lucida Grande\" size=\"-1\"><p>Notice that in this example, every time you "
						+ "try to give all your money away, you will result in a gain of $E 0, and at the origin, you also result in a gain of $E 0. "
						+ "To be more accurate with how much you want to give, you can also enter numbers into the input fields as well. Just be sure to "
						+ "hit ENTER when you want the pledge to accept your input. You will know your input is accepted when the background of the input boxes "
						+ "returns to white from yellow. "
						+ "To gain the most amount of money, you will need to search for an ideal pledge to make for each round. "
						+ "Your task is to explore all the options and pick the pledge you like <b><u>most</u></b> for each round.");
		instructions[4]
				.setText("<html><font face=\"Lucida Grande\" size=\"-1\">"
						+ "<p>During the experiment, we will give you multiple rounds to decide on pledges like that above, asking you to select "
						+ "your most preferred pledge for each round."
						+ "<p>After everyone has made their choices, your gains will be determined as follows. We will check each pledge against "
						+ "every other in order to make sure you are properly matched. If you are not matched, your pledged will be scaled down based on "
						+ "your given rate until it matches, or becomes $E 0. Finally, if you were matched fully, you should see a gain equal to what " 
						+ "was printed on the bottom of your pledge. Otherwise, you will see a number less than that if you were scaled down, or possibly more " 
						+ "than that if someone made a pledge that gave more than was required to match. "
						+ "<p>Thus you should notice that each gain will be based on some function accounting for the money you have left, "
						+ "the money others have given, and of course, your \"thank you\" payment.");

		text.add(title);
		text.add(instructions[0]);
		text.add(screenshot); // Add screen shot of Gain example
		text.add(instructions[1]);
		text.add(instructions[2]);
		text.add(demo); //Add demo of Gain example 
		text.add(instructions[3]);
		text.add(instructions[4]);

		scroll = new JScrollPane(text);
		text.setPreferredSize(new Dimension(1001, 2000));
		scroll.setMinimumSize(new Dimension(1018, 800));
		scroll.setMaximumSize(new Dimension(1018, 800));
		scroll
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setBorder(BorderFactory.createLineBorder(Color.black));

		constraints.fill = GridBagConstraints.VERTICAL;
		constraints.weightx = 1;
		constraints.weighty = 1;
		layout.setConstraints(scroll, constraints);

		splash = new JLabel(
				"<html><center><font size=+3>Welcome to the EconLab at UCSD</font>"
						+ "<p><p><font size=+2>Please wait for instructions before continuing</font></center></html>");

		centerPanel.add(splash);

		// bottom
		bottom = new JPanel();
		bottom.setBackground(Color.white);
		bottom.setOpaque(true);

		start = new JButton("Wait before continuing");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((Instructions) ((JButton) e.getSource()).getTopLevelAncestor())
						.goToInstructions();
			}
		});
		bottom.add(start);

		back = new JButton("Back");

		mainPanel.add(top, BorderLayout.NORTH);
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		mainPanel.add(bottom, BorderLayout.SOUTH);

		add(mainPanel);
		setVisible(true);

	}

	public void waitForNumber() {

		mainPanel.remove(centerPanel);
		mainPanel.remove(bottom);
		centerPanel = new JPanel();
		centerPanel.setBackground(Color.white);
		centerPanel.setOpaque(true);
		GridBagLayout layout2 = new GridBagLayout();
		GridBagConstraints constraints2 = new GridBagConstraints();
		JLabel jl = new JLabel(
				"<html><font size=+5>Please Wait Until Everyone has Finished</font></html>");
		centerPanel.setLayout(layout2);
		layout2.setConstraints(jl, constraints2);
		centerPanel.add(jl);
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		validate();
	}

	private void goToInstructions() {
		centerPanel.remove(splash);
		centerPanel.add(scroll);
		start.removeActionListener(start.getActionListeners()[0]);
		
		if(testing){
			ActionListener newAL = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Instructions inst = (Instructions) (((JComponent) e
							.getSource()).getTopLevelAncestor());
					inst.setVisible(false);
					inst.client.startDecisionMaker();
				}
			};
			// Add new listener to Start
			start.addActionListener(newAL);			
		} else {
			start.addActionListener(new RunQuiz(RunQuiz.FIRST_PAGE));
		}
		
		/*This skips the instructions quiz because it slows down testing*/
		/* create new AL */

		
		scroll.getVerticalScrollBar().getModel().addChangeListener(
				new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						if (((BoundedRangeModel) e.getSource()).getMaximum()
								- ((BoundedRangeModel) e.getSource())
										.getExtent() == ((BoundedRangeModel) e
								.getSource()).getValue()) {
							canAdvance[0].setValue(true);
							start.setEnabled(canAdvance[0].getValue());
						}
					}
				});
		start.setText("Continue");
		start.setEnabled(false);
		validate();
		repaint();
	}

	private class RunQuiz implements ActionListener {
		public static final int INSTRUCTIONS = 0;
		public static final int FIRST_PAGE = 1;
		public static final int SECOND_PAGE = 2;
		public static final int THIRD_PAGE = 3;
		public static final int FOURTH_PAGE = 4;

		private String[] fileLocation = { "", "/images/test1.jpg",
				"/images/test3.jpg"};

		private int page;

		public RunQuiz(int pageOf) {
			page = pageOf;
		}

		public void actionPerformed(ActionEvent e) {

			Instructions inst = (Instructions) (((JComponent) e.getSource())
					.getTopLevelAncestor());

			QuizPanel quiz;
			JTextPane[] text;
			String[][] answers;
			String newButtonText = "";
			ActionListener newAL;

			/* Remove all existing ActionListeners from calling button */
			ActionListener[] listeners = inst.start.getActionListeners();
			for (ActionListener listener : listeners) {
				inst.start.removeActionListener(listener);
			}

			listeners = inst.back.getActionListeners();
			for (ActionListener listener : listeners) {
				inst.back.removeActionListener(listener);
			}

			/* Remove current contents of inst.centerPanel */
			Component[] oldContents = inst.centerPanel.getComponents();
			for (Component comp : oldContents) {
				inst.centerPanel.remove(comp);
			}

			/* Create specified quiz panel */
			switch (page) {
			case INSTRUCTIONS:
				oldContents = inst.bottom.getComponents();
				for (Component comp : oldContents) {
					inst.bottom.remove(comp);
				}

				newButtonText = "Continue";
				inst.start.addActionListener(new RunQuiz(RunQuiz.FIRST_PAGE));
				inst.start.setEnabled(inst.canAdvance[INSTRUCTIONS].getValue());

				inst.bottom.add(inst.start);
				inst.centerPanel.add(inst.scroll);

				inst.centerPanel.validate();
				inst.centerPanel.repaint();

				inst.bottom.validate();
				inst.bottom.repaint();

				inst.validate();
				inst.repaint();

				break;

			case FIRST_PAGE:
				
				/*JTextPane[] title = new JTextPane[1];
				title[0].setAlignmentY(Component.TOP_ALIGNMENT);
				title[0].setAlignmentX(Component.CENTER_ALIGNMENT);
				title[0].setEditable(false);
				title[0].setContentType("text/html");
				title[0].setText("<b><font size=+3>TESTING YOUR UNDERSTANDING</font><b>");*/

				/* Create parameters to QuizPanel constructor */
				text = new JTextPane[3];
				for (int i = 0; i < text.length; i++) {
					text[i] = new JTextPane();
					text[i].setAlignmentX(Component.CENTER_ALIGNMENT);
					text[i].setEditable(false);
					text[i].setContentType("text/html");
				}

				text[0]
						.setText("<font size=+2>TESTING YOUR UNDERSTANDING</font>" +
								 "<p>Suppose we have chosen this pledge to be the one that counts, " +
								 "and that you have decided that the most you would like to give " +
								 "is $E 1000.00 in hopes of <font color=\"green\">gaining</font> $US 3000.00</p>");
				text[1]
						.setText("<p>If others gave $E 2000.00, how much would you gain? Do NOT include the \"thank you\" payment.</p>");
				text[2]
						.setText("<p>If others gave $E 0.00, how much would you gain? Do NOT include the \"thank you\" payment.</p>");

				answers = new String[2][0];

				answers[0] = new String[1];
				answers[0][0] = "3000";
				answers[1] = new String[1];
				answers[1][0] = "0";

				/* Create new quiz panel */
				quiz = new QuizPanel(fileLocation[FIRST_PAGE], text, answers,
						canAdvance[FIRST_PAGE]);
				/* Add the new QuizPanel to the Instructions */
				inst.centerPanel.add(quiz);

				/* create new AL */
				newAL = new RunQuiz(RunQuiz.SECOND_PAGE);
				newButtonText = "Continue to Part 2";
				/* Add new listener to Start */
				inst.start.addActionListener(newAL);
				inst.start.setEnabled(inst.canAdvance[FIRST_PAGE].getValue());

				newAL = new RunQuiz(RunQuiz.INSTRUCTIONS);
				inst.back.addActionListener(newAL);
				inst.bottom.add(inst.back);

				inst.centerPanel.validate();
				inst.centerPanel.repaint();

				inst.bottom.validate();
				inst.bottom.repaint();

				break;

			case SECOND_PAGE:
				/* Create parameters to QuizPanel constructor */
				
				/*//self added
				JTextPane[] title2 = new JTextPane[1];
				title2[0].setAlignmentY(Component.TOP_ALIGNMENT);
				title2[0].setAlignmentX(Component.CENTER_ALIGNMENT);
				title2[0].setEditable(false);
				title2[0].setContentType("text/html");
				title2[0].setText("<b><font size=+3>TESTING YOUR UNDERSTANDING</font><b>");
				//ends here*/
				
				text = new JTextPane[4];
				for (int i = 0; i < text.length; i++) {
					text[i] = new JTextPane();
					text[i].setAlignmentX(Component.CENTER_ALIGNMENT);
					text[i].setEditable(false);
					text[i].setContentType("text/html");
				}

				text[0]
						.setText("<font size=+2>TESTING YOUR UNDERSTANDING</font>" + 
								"<p>Suppose we have chosen this pledge to be the one that counts, " +
								 "and that you have decided that the most you would like to give " +
								 "is $E 100.00 in hopes of <font color=\"green\">gaining</font> $US 1100.00</p>");
				text[1]
						.setText("<p>If others gave $E 1000.00, how much would you gain? Do NOT include the \"thank you\" payment.</p>");
				text[2]
						.setText("<p>If others gave $E 0.00, how much would you gain? Do NOT include the \"thank you\" payment.</p>");

				answers = new String[2][0];

				answers[0] = new String[1];
				answers[0][0] = "1100";
				answers[1] = new String[1];
				answers[1][0] = "0";

				/* Create new quiz panel */
				//title2 added by me
				quiz = new QuizPanel(fileLocation[SECOND_PAGE], text, answers,
						canAdvance[SECOND_PAGE]);
				/* Add the new QuizPanel to the Instructions */
				inst.centerPanel.add(quiz);

				/* change AL */		
				newAL = new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Instructions inst = (Instructions) (((JComponent) e
								.getSource()).getTopLevelAncestor());
						inst.setVisible(false);
						inst.client.startDecisionMaker();
					}
				};
				
				newButtonText = "Finished";
				/* Add new listener to Start */
				inst.start.addActionListener(newAL);
				inst.start.setEnabled(inst.canAdvance[SECOND_PAGE].getValue());

				newAL = new RunQuiz(RunQuiz.FIRST_PAGE);
				inst.back.addActionListener(newAL);

				inst.centerPanel.validate();
				inst.centerPanel.repaint();

				break;

			

			default:
				break;
			}
			// inst.start.setEnabled(true);
			inst.start.setText(newButtonText);
		}
	}

	private class CheckAnswer implements ActionListener {

		private static final int TEXTFIELD_TYPE = -1;
		private static final int CHECKBOX_TYPE = -2;

		QuizPanel panel;
		JTextField field;
		CheckBox[] box;
		String answer;
		int type;

		/*
		 * Creates an instance of CheckAnswer to be used for a question who
		 * answer is entered into a JTextField
		 */
		public CheckAnswer(JTextField answerBox, String correctAnswer,
				QuizPanel panel) {
			field = answerBox;
			answer = correctAnswer;
			type = TEXTFIELD_TYPE;
			this.panel = panel;
		}

		/*
		 * Creates an instance of CheckAnswer to be used for a question who
		 * answer(s) are selected from options in a JCheckbox[].
		 */
		public CheckAnswer(CheckBox[] boxes, QuizPanel panel) {
			box = boxes;
			type = CHECKBOX_TYPE;
			this.panel = panel;
		}

		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton) ((JComponent) e.getSource());

			if (type == TEXTFIELD_TYPE) { // Question is a text field question
				String answerGiven = field.getText();

				if (answerGiven.trim().equals(answer)
						|| answerGiven.trim().equals("$E " + answer)
						|| answerGiven.trim().equals("$E " + answer+".00")
						|| answerGiven.trim().equals(answer+".00")
						|| answerGiven.trim().equals(answer+".0")
						|| answerGiven.trim().equals("$ "+answer+".0")
						|| answerGiven.trim().equals("$ "+answer+".00")
						|| (answer.length() == 2 && (answerGiven.trim().equals(
								"$E " + answer + ".00") || answerGiven.trim()
								.equals("$E " + answer + ".0") || answerGiven.trim()
								.equals(answer + ".00") || answerGiven.trim()
								.equals(answer + ".0")))
						|| (answer.length() == 4 && (answerGiven.trim().equals(
								"$" + answer + "0") || answerGiven.trim()
								.equals(answer + "0")))) {
					button.setText("Correct!");
					button.setForeground((Color.GREEN).darker());
					if (panel.canAdvance()) {
						((Instructions) panel.getTopLevelAncestor()).start
								.setEnabled(true);
						panel.allCorrect.setValue(true);
					}
				} else {
					button.setText("Try Again!");
					button.setForeground(Color.RED);
				}
			} else {
				// Question is a check-box question
				/*
				 * check the boxes against the boxes that are supposed to be
				 * selected.
				 */
				boolean correct = true;

				for (int i = 1; i < box.length; i++) {
					if (box[i].isSelected() != box[i].shouldBeSelected()) {
						correct = false;
					}
				}

				if (correct) {
					button.setText("Correct!");
					button.setForeground((Color.GREEN).darker());
					if (panel.canAdvance()) {
						((Instructions) panel.getTopLevelAncestor()).start
								.setEnabled(true);
						panel.allCorrect.setValue(true);
					}
				} else {
					button.setText("Try Again!");
					button.setForeground(Color.RED);
				}
			}
			repaint();
			validate();
		}
	}

	private class QuizPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String imageLocation;
		JTextPane[] title;
		JTextPane[] questions;
		String[][] answers;
		int numberOfQuestions;
		betterBoolean allCorrect;

		JLabel screenshot;
		JScrollPane quiz;

		JTextField[] answer_box;
		CheckBox[][] check_box;
		JButton[] checkButton;

		/*
		 * Creates a new instance of the class QuizPanel imageFile: location of
		 * the screenshot image showing the example questions: an array holding
		 * the instructions in questions[0] and questions in questions[1] -
		 * questions[questions.length - 1] questions[0] should be initialized to
		 * null if there are no instructions. answers: an array holding the
		 * answers to the questions. If questions[n] is a text field question,
		 * answers[n-1] will contain the answer If it is a check box questions,
		 * answers[n-1] will be an array of Strings holding the options. Correct
		 * answers will be indicated by the presence of "!!" at the beginning of
		 * the String.
		 */
		public QuizPanel(String imageFile, JTextPane[] questions,
				String[][] answers, betterBoolean allCorrect) {
			super();

			//this.title = title;
			this.questions = questions;
			this.answers = answers;
			this.imageLocation = imageFile;
			this.allCorrect = allCorrect;
			numberOfQuestions = answers.length;

			/*this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			JPanel titlep = new JPanel();
			titlep.add(title);
			this.add(title);*/
			this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			this.setBackground(Color.white);
			this.setOpaque(true);

			createScreenshot();
			createQuiz();

			this.add(screenshot);
			this.add(quiz);
		}

		private void createScreenshot() {
			screenshot = new JLabel(new ImageIcon(getClass().getResource(imageLocation)));
			screenshot.setPreferredSize(new Dimension(606, 556));
			screenshot.setMinimumSize(new Dimension(606, 556));
			screenshot.setMaximumSize(new Dimension(606, 556));
		}

		private void createQuiz() {

			/* Create and format a JPanel to hold questions, answering options */
			JPanel content = new JPanel();
			content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
			content.setBackground(Color.white);
			content.setOpaque(true);
			content.setPreferredSize(new Dimension(618, 400));
			content.setMinimumSize(new Dimension(618, 500));
			content.setMaximumSize(new Dimension(618, 500));
			content.setBorder(BorderFactory.createLineBorder(Color.black));

			/* Create vertical strut to space questions and answer fields */
			Component spacer = Box.createVerticalStrut(8);

			/* Add instructions, if any, to the top of the container. */
			if (questions[0] != null) {
				content.add(questions[0]);
			}

			JPanel[] answering = new JPanel[numberOfQuestions]; /*
																 * I want a new
																 * name for
																 * this,
																 * answering
																 * doesn't make
																 * sense
																 */
			answer_box = new JTextField[numberOfQuestions];
			check_box = new CheckBox[numberOfQuestions][];
			checkButton = new JButton[numberOfQuestions];

			for (int i = 0; i < numberOfQuestions; i++) {
				answering[i] = new JPanel();
				answering[i].setOpaque(false);
				answering[i].setLayout(new BoxLayout(answering[i],
						BoxLayout.LINE_AXIS));
				answering[i].setMinimumSize(new Dimension(618, 30));
				answering[i].setAlignmentX(Component.LEFT_ALIGNMENT);
				answering[i].setAlignmentY(Component.TOP_ALIGNMENT);

				checkButton[i] = new JButton("Check Answer");
				checkButton[i].setAlignmentX(Component.LEFT_ALIGNMENT);
				checkButton[i].setAlignmentY(Component.TOP_ALIGNMENT);
				if (allCorrect.getValue()) {
					checkButton[i].setText("Correct!");
					checkButton[i].setForeground((Color.GREEN).darker());
				}

				if (answers[i].length == 1) { /* TextField question */
					answer_box[i] = new JTextField();
					answer_box[i].setPreferredSize(new Dimension(150, 27));
					answer_box[i].setMinimumSize(new Dimension(150, 27));
					answer_box[i].setMaximumSize(new Dimension(150, 27));
					answer_box[i].setAlignmentX(Component.LEFT_ALIGNMENT);
					answer_box[i].setAlignmentY(Component.TOP_ALIGNMENT);
					if (allCorrect.getValue()) {
						answer_box[i].setText(answers[i][0]);
					}
					/*
					 * add appropriate CheckAnswer listener to this question's
					 * button
					 */
					checkButton[i].addActionListener(new CheckAnswer(
							answer_box[i], answers[i][0], this));

					/* add answer box to the quiz */
					answering[i].add(answer_box[i]);
				} else { /* CheckBox question */
					answering[i].setLayout(new BoxLayout(answering[i],
							BoxLayout.PAGE_AXIS));

					check_box[i] = new CheckBox[answers[i].length];

					/* Process answers */
					for (int c = 0; c < check_box[i].length; c++) {

						boolean trueOrFalse = false;

						/* Determine if this box will display a correct answer. */
						if (answers[i][c].startsWith("!!")) {
							String temp = answers[i][c];
							answers[i][c] = temp.substring(2);
							trueOrFalse = true;
						}

						/* Create new CheckBox */
						check_box[i][c] = new CheckBox(answers[i][c],
								trueOrFalse);
						check_box[i][c].setSelected(false);
						check_box[i][c].setOpaque(false);
						if (allCorrect.getValue() && trueOrFalse) {
							check_box[i][c].setSelected(true);
						}

						/* Add check box to quiz */
						answering[i].add(check_box[i][c]);

					}

					/*
					 * add appropriate CheckAnswer listener to this question's
					 * button
					 */
					checkButton[i].addActionListener(new CheckAnswer(
							check_box[i], this));
				}

				answering[i].add(checkButton[i]);

				content.add(questions[i + 1]);
				content.add(spacer);
				content.add(answering[i]);
				content.add(spacer);
				content.add(spacer);
			}

			if (imageLocation.equals("/images/firstQuizImage.gif")
					|| imageLocation.equals("/images/secondQuizImage.gif")) {
				content.add(Box.createVerticalStrut(310));
			}

			/* Create and format JScrollPane to wrap the entire quiz */
			quiz = new JScrollPane(content);
			quiz.setPreferredSize(new Dimension(618, 500));
			quiz.setMinimumSize(new Dimension(618, 500));
			quiz.setMaximumSize(new Dimension(618, 500));
			quiz.setBorder(BorderFactory.createLineBorder(Color.black));
			quiz
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			quiz
					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		}

		public boolean canAdvance() {
			for (JButton button : checkButton) {
				if (!button.getText().equals("Correct!")) {
					return false;
				}
			}
			return true;
		}
	}

	private class CheckBox extends JCheckBox {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private boolean isSelected;

		public CheckBox(String text, boolean isCorrect) {
			super(text);
			isSelected = isCorrect;
		}

		public boolean shouldBeSelected() {
			return isSelected;
		}
	}

	private class betterBoolean {

		boolean value;

		public betterBoolean(boolean value) {
			this.value = value;
		}

		public boolean getValue() {
			return value;
		}

		public void setValue(boolean value) {
			this.value = value;
		}
	}
}
